class ListNode:
    def __init__(self,val,next = None):
        self.val = val
        self.next = next


class Solution:
    def getlenthofListnode(self, head):
        a = 0
        while head:
            if head == None:
                return a
            a += 1
            head = head.next

        return a

    def FindFirstCommonNode(self, pHead1, pHead2):
        # write code here
        lphead1 = self.getlenthofListnode(pHead1)
        lphead2 = self.getlenthofListnode(pHead2)

        k = lphead1 - lphead2

        fast = pHead1
        slow = pHead2

        if k >= 0:
            for i in range(k):
                fast = fast.next
        else:
            for i in range(-k):
                slow = slow.next

        while fast:
            if fast == slow:
                return fast
            fast = fast.next
            slow = slow.next


        return None


def printList(node: ListNode):
    while node:
        print(node.val, end="->" if node.next else "\n")
        node = node.next

if __name__ == "__main__":
    node1 = ListNode(1)
    node2 = ListNode(2)
    node3 = ListNode(3)
    node4 = ListNode(4)
    node5 = ListNode(5)
    node6 = ListNode(6)
    node7 = ListNode(7)

    node1.next = node2
    node2.next = node3
    node3.next = node6
    node6.next = node7

    node4.next = node5
    node5.next = node6

    solu = Solution()

    result = solu.FindFirstCommonNode(node1,node1)
    printList(result)